import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yh_core_flutter_project/project/common/constant/colors.dart';
import 'package:yh_core_flutter_project/project/router/app_routers.dart';
import 'package:yh_core_flutter_project/project/common/managers/user_manager.dart';

class WelcomePage extends StatefulWidget {
  const WelcomePage({Key? key}) : super(key: key);

  @override
  State<WelcomePage> createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> {
  final PageController _pageController = PageController();
  int _currentPage = 0;

  final List<WelcomeItem> _items = [
    WelcomeItem(title: '欢迎使用智能家居', description: '让您的家更智能、更舒适、更节能', image: 'assets/images/png/common/bg_clock.png'),
    WelcomeItem(title: '设备管理', description: '轻松添加、控制和管理您的智能设备', image: 'assets/images/png/common/img_success.png'),
    WelcomeItem(title: '智能场景', description: '创建个性化场景，一键控制多个设备', image: 'assets/images/png/home/img_empty_device.png'),
  ];

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  void _onPageChanged(int page) {
    setState(() {
      _currentPage = page;
    });
  }

  void _onNextPressed() {
    if (_currentPage < _items.length - 1) {
      _pageController.nextPage(duration: const Duration(milliseconds: 300), curve: Curves.easeInOut);
    } else {
      _onGetStarted();
    }
  }

  void _onGetStarted() async {
    await Get.find<UserManager>().markWelcomeCompleted();
    Get.offAllNamed(AppRouters.login.r);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          children: [
            Align(
              alignment: Alignment.topRight,
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: TextButton(onPressed: _onGetStarted, child: Text('跳过', style: TextStyle(color: Colors.grey[600], fontSize: 16))),
              ),
            ),
            Expanded(
              child: PageView.builder(
                controller: _pageController,
                onPageChanged: _onPageChanged,
                itemCount: _items.length,
                itemBuilder: (context, index) {
                  return _buildPage(_items[index]);
                },
              ),
            ),
            _buildPageIndicator(),
            Padding(
              padding: const EdgeInsets.all(24.0),
              child: SizedBox(
                width: double.infinity,
                height: 56,
                child: ElevatedButton(
                  onPressed: _onNextPressed,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.black87,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
                  ),
                  child: Text(_currentPage == _items.length - 1 ? '开始使用' : '下一步', style: const TextStyle(fontSize: 18,color: Colors.white, fontWeight: FontWeight.w600)),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildPage(WelcomeItem item) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 32.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(flex: 3, child: Image.asset(item.image, width: 100, height: 100, fit: BoxFit.contain)),
          Expanded(
            flex: 2,
            child: Column(
              children: [
                Text(item.title, style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold, color: Colors.black87), textAlign: TextAlign.center),
                const SizedBox(height: 16),
                Text(item.description, style: TextStyle(fontSize: 16, color: Colors.grey[600], height: 1.5), textAlign: TextAlign.center),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildPageIndicator() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List.generate(
        _items.length,
        (index) => AnimatedContainer(
          duration: const Duration(milliseconds: 300),
          margin: const EdgeInsets.symmetric(horizontal: 4),
          width: _currentPage == index ? 24 : 8,
          height: 8,
          decoration: BoxDecoration(color: _currentPage == index ? AppColor.primary : Colors.black87, borderRadius: BorderRadius.circular(4)),
        ),
      ),
    );
  }
}

class WelcomeItem {
  final String title;
  final String description;
  final String image;

  WelcomeItem({required this.title, required this.description, required this.image});
}
